﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using Maticsoft.DBUtility;//请先添加引用
namespace Usas.DAL.Cms
{
    /// <summary>
    /// 数据访问类cms_block_common_detail。
    /// </summary>
    public class cms_block_common_detail
    {
        public cms_block_common_detail()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int c_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from cms_block_common_detail");
            strSql.Append(" where c_id=" + c_id + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Usas.Model.Cms.cms_block_common_detail model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.c_b_id != null)
            {
                strSql1.Append("c_b_id,");
                strSql2.Append("" + model.c_b_id + ",");
            }
            if (model.c_type != null)
            {
                strSql1.Append("c_type,");
                strSql2.Append("'" + model.c_type + "',");
            }
            if (model.c_value != null)
            {
                strSql1.Append("c_value,");
                strSql2.Append("'" + model.c_value + "',");
            }
            if (model.c_des != null)
            {
                strSql1.Append("c_des,");
                strSql2.Append("'" + model.c_des + "',");
            }
            if (model.c_view != null)
            {
                strSql1.Append("c_view,");
                strSql2.Append("" + model.c_view + ",");
            }
            strSql.Append("insert into cms_block_common_detail(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Usas.Model.Cms.cms_block_common_detail model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update cms_block_common_detail set ");
            if (model.c_b_id != null)
            {
                strSql.Append("c_b_id=" + model.c_b_id + ",");
            }
            if (model.c_type != null)
            {
                strSql.Append("c_type='" + model.c_type + "',");
            }
            if (model.c_value != null)
            {
                strSql.Append("c_value='" + model.c_value + "',");
            }
            if (model.c_des != null)
            {
                strSql.Append("c_des='" + model.c_des + "',");
            }
            if (model.c_view != null)
            {
                strSql.Append("c_view=" + model.c_view + ",");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where c_id=" + model.c_id + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int c_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from cms_block_common_detail ");
            strSql.Append(" where c_id=" + c_id + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Usas.Model.Cms.cms_block_common_detail GetModel(int c_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" c_id,c_b_id,c_type,c_value,c_des,c_view ");
            strSql.Append(" from cms_block_common_detail ");
            strSql.Append(" where c_id=" + c_id + " ");
            Usas.Model.Cms.cms_block_common_detail model = new Usas.Model.Cms.cms_block_common_detail();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["c_id"].ToString() != "")
                {
                    model.c_id = int.Parse(ds.Tables[0].Rows[0]["c_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["c_b_id"].ToString() != "")
                {
                    model.c_b_id = int.Parse(ds.Tables[0].Rows[0]["c_b_id"].ToString());
                }
                model.c_type = ds.Tables[0].Rows[0]["c_type"].ToString();
                model.c_value = ds.Tables[0].Rows[0]["c_value"].ToString();
                model.c_des = ds.Tables[0].Rows[0]["c_des"].ToString();
                if (ds.Tables[0].Rows[0]["c_view"].ToString() != "")
                {
                    model.c_view = int.Parse(ds.Tables[0].Rows[0]["c_view"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select c_id,c_b_id,c_type,c_value,c_des,c_view ");
            strSql.Append(" FROM cms_block_common_detail ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" c_id,c_b_id,c_type,c_value,c_des,c_view ");
            strSql.Append(" FROM cms_block_common_detail ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
          */
        public int SetNewDetail(string WhereDelRows, List<Model.Cms.cms_block_common_detail> ls)
        {

            List<string> l = new List<string>();
            l.Add("delete from cms_block_common_detail where " + WhereDelRows);

            for (int i = 0; i < ls.Count; i++)
            {
                l.Add("insert into cms_block_common_detail values(" + ls[i].c_b_id + ",'" + ls[i].c_type + "','" +
                    ls[i].c_value + "','" + ls[i].c_des + "'," + ls[i].c_view + ")");
            }

            return DbHelperSQL.ExecuteSqlTran(l);
        }
        #endregion  成员方法
    }
}

